<template>
  <div>
    <div class="container">
      <div class="handle-box">
        <common-item1
          :query="query"
          :streetList="streetList"
          :communityList="communityList"
          :villageList="villageList"
          @visibleChangeStreet="visibleChangeStreet"
          @changeStreet="changeStreet"
          @visibleChangeCommunity="visibleChangeCommunity"
          @changeCommunity="changeCommunity"
          @visibleChangeVillage="visibleChangeVillage"
          @changeVillage="changeVillage"
          :showInput="showInput"
          :show="false"
        />
        <el-input
          v-model="query.equipNo"
          placeholder="设备编号"
          class="handle-input mr10"
          @keyup.native.enter="handleSearch()"
        ></el-input>
        <el-button type="primary" @click="handleSearch">查询</el-button>
        <el-button
          type="danger"
          class="handle-del mr10"
          @click="delAllSelection"
          v-if="smPermissions.includes('village:balance:delete')"
          >删除</el-button
        >
      </div>
      <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        header-cell-class-name="table-header"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="55"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="index"
          label="序号"
          width="55"
          align="center"
        ></el-table-column>
        <el-table-column prop="equipNo" label="设备编号"></el-table-column>

        <el-table-column prop="subDistrict" label="所属街道"></el-table-column>

        <el-table-column
          prop="communityName"
          label="所属社区"
        ></el-table-column>
        <el-table-column prop="villageName" label="所属小区"></el-table-column>
        <el-table-column prop="equipType" label="秤类型"></el-table-column>
        <el-table-column prop="createDate" label="创建时间"></el-table-column>
        <el-table-column prop="memo" label="称说明"></el-table-column>

        <el-table-column label="称状态" align="center">
          <template slot-scope="scope">
            <el-tag
              :type="
                scope.row.state === '0'
                  ? 'success'
                  : scope.row.state === '1'
                  ? 'danger'
                  : ''
              "
              >{{ scope.row.statusName }}</el-tag
            >
          </template>
        </el-table-column>

        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="handleEdit(scope.$index, scope.row)"
              v-if="smPermissions.includes('village:balance:update')"
              >编辑</el-button
            >
            <el-button
              type="text"
              class="red"
              @click="handleDelete(scope.$index, scope.row)"
              v-if="smPermissions.includes('village:balance:delete')"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          layout="total,sizes, prev, pager, next, jumper"
          :page-sizes="[5, 10, 20, 30]"
          @size-change="handleSizeChange"
          :current-page="query.pageIndex"
          :page-size="query.pageSize"
          :total="pageTotal"
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
    </div>

    <!-- 编辑弹出框 -->
    <el-dialog title="编辑" :visible.sync="editVisible" width="50%">
      <el-form ref="form" :model="form" label-width="auto">
        <el-form-item label="用户名">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="form.address"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveEdit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { commonItem1 } from "../../common/commonItem1/common-item1.js";
export default {
  name: "scalemanage",
  data() {
    return {
      smPermissions: [],
      query: {
        address: "",
        name: "",
        pageIndex: 1,
        pageSize: 20,
        equipNo: "",
        streetId: "",
        communityId: "",
        villageId: "",
        streetName: "",
        communityName: "",
        villageName: "",
      },
      showInput: true,
      tableData: [],
      streetList: [],
      communityList: [],
      villageList: [],
      // villageId: "",
      delList: [],
      editVisible: false,
      pageTotal: 0,
      form: {},
      idx: -1,
      id: -1,
    };
  },
  created() {
    this.getData();
    // 获取街道数据
    // this.getStreetDate();
    this.smPermissions = this.permissions;
  },
  components: {
    "common-item1": () => import("../../common/commonItem1/common-item1.vue"),
  },
  computed: {
    ...mapGetters(["permissions"]),
  },
  watch: {
    "$route.path": function(newVal, oldVal) {
      if (newVal == "/scale-manage") {
        this.smPermissions = this.permissions;
      }
    },
    // 刷新浏览器时触发
    permissions: function(newVal, oldVal) {
      this.smPermissions = newVal;
    },
  },
  methods: {
    // handleKeyword(keyword) {
    //   console.log(keyword);
    //   this.query.villageName = keyword;
    // },
    ...commonItem1,

    getData() {
      this.$axios
        .get("/village/balance/page", {
          params: {
            page: this.query.pageIndex,
            limit: this.query.pageSize,
            villageId: this.query.villageId,
            equipNo: this.query.equipNo,
            villageName:
              this.showInput == false ? "" : this.query.villageName.trim(),
          },
        })
        .then(({ data: res }) => {
          console.log("秤管理数据", res);
          if (res.code !== 0) {
            return;
          }
          this.tableData = res.data.list;
          this.pageTotal = res.data.total;
          this.tableData.map((item, index) => {
            var pageIndex = this.query.pageIndex;
            item.index = index + 1 + (pageIndex - 1) * this.query.pageSize;
            item.createDate = this.dateFormat(item.createDate);
            if (item.enableStatus == 0) {
              item.statusName = "正常";
            } else if (item.enableStatus == 1) {
              item.statusName = "停用";
            }
          });
        })
        .catch(() => {});
    },
    // 触发搜索按钮
    handleSearch() {
      this.$set(this.query, "pageIndex", 1);
      if (
        this.query.villageName.trim() == "" &&
        this.query.villageId.trim() == "" &&
        this.query.equipNo == ""
      ) {
        return this.$message.warning({
          message: "请先选择小区",
          duration: 1500,
        });
      }
      this.getData();
    },
    // 删除操作
    handleDelete(index, row) {
      this.ids = [];
      var ids = this.ids;
      ids[0] = row.id;
      // 二次确认删除
      this.$confirm("确定要删除吗？", "提示", {
        type: "warning",
      })
        .then(() => {
          this.delete(ids);
        })
        .catch(() => {});
    },
    delete(ids) {
      this.$axios
        .delete("/village/balance", {
          data: ids,
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return;
          }
          this.$message.success("操作成功");
          this.ids = [];
          this.getData();
        })
        .catch(() => {});
    },
    // 多选操作
    handleSelectionChange(val) {
      this.ids = [];
      var ids = this.ids;
      val.forEach((item, index) => {
        ids.push(item.id);
      });
    },
    delAllSelection() {
      var ids = this.ids;
      ids = [...new Set(ids)];
      this.$confirm("确定要删除吗？", "提示", {
        type: "warning",
      })
        .then(() => {
          this.delete(ids);
        })
        .catch(() => {});
    },
    // 编辑操作
    handleEdit(index, row) {
      this.idx = index;
      this.form = row;
      this.editVisible = true;
    },
    // 保存编辑
    saveEdit() {
      this.editVisible = false;
      this.$message.success(`修改第 ${this.idx + 1} 行成功`);
      this.$set(this.tableData, this.idx, this.form);
    },
    // 分页导航
    handlePageChange(val) {
      this.$set(this.query, "pageIndex", val);
      this.getData();
    },
    handleSizeChange(val) {
      this.$set(this.query, "pageSize", val);
      this.getData();
    },
  },
};
</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 120px;
  display: inline-block;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
</style>
